package com.zoffy.ybuhelper.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.zoffy.ybuhelper.R;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public abstract class ItemControlRecyclerAdapter extends RecyclerView.Adapter<ItemControlRecyclerAdapter.ControlViewHolder> {
    protected List<?> dataList;
    private SetItemOnclick setItemOnclick;
    private Boolean show=false;

    public ItemControlRecyclerAdapter(List<?> dataList) {
        this.dataList = dataList;
    }

    @NonNull
    @NotNull
    @Override
    public ControlViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_control, parent, false);
        return new ControlViewHolder(view);
    }

    protected abstract void setItemData(@NonNull @NotNull ControlViewHolder holder,int position);
    @Override
    public void onBindViewHolder(@NonNull @NotNull ControlViewHolder holder, int position) {
        setItemData(holder,position);
//        holder.originTextView.setText(eat.getName());
        if (setItemOnclick != null){
            holder.deleteButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setItemOnclick.onDeleteClick(holder.getLayoutPosition());
                }
            });
            holder.changeButton.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    setItemOnclick.onChangeClick(holder.getLayoutPosition());
                }
            });
        }
        holder.deleteButton.setVisibility(show?View.VISIBLE:View.GONE);
    }

    public Object getDataListAt(int position){
        return dataList.get(position);
    }
    @Override
    public int getItemCount() {
        return dataList.size();
    }
    public Object getItemAt(int position){
        return dataList.get(position);
    }



    public interface SetItemOnclick {
        void onDeleteClick(int position);

        void onChangeClick(int position);
    }

    protected class ControlViewHolder extends RecyclerView.ViewHolder {
        protected TextView nameTextView, originTextView, informationTextView,deletedTextView;
        private Button deleteButton, changeButton;

        public ControlViewHolder(@NotNull View itemView) {
            super(itemView);
            nameTextView = itemView.findViewById(R.id.item_name);
            originTextView = itemView.findViewById(R.id.origin_text);
            informationTextView = itemView.findViewById(R.id.item_information);
            deleteButton = itemView.findViewById(R.id.delete);
            changeButton = itemView.findViewById(R.id.change);
            deletedTextView=itemView.findViewById(R.id.deleted_text);
        }
    }
    public void changeCouldDelete(){
        show=!show;
        notifyDataSetChanged();
    }


    public void deleteItem(int position){
        dataList.remove(position);
    }

    public void setSetItemOnclick(SetItemOnclick setItemOnclick) {
        this.setItemOnclick = setItemOnclick;
    }

}
